//
//  VisitorView
//  HMWeibo07
//
//  Created by Xwoder on 15/7/28.
//  Copyright © 2015年 Xwoder. All rights reserved.
//

import UIKit

@objc
protocol VisitorViewDelegate: NSObjectProtocol {
    optional func visitorViewLoginButtonClicked()
    optional func visitorViewRegisterButtonClicked()
}

class VisitorView: UIView {
    
    // 是否为homeTableViewController
    var isHome: Bool?
    
    weak var delegate: VisitorViewDelegate?

    // MARK: 设置视图信息
    func setupViewInfo(isHome isHome: Bool, imageName: String, message: String) {
        
        // 设置文本标签
        messageLabel.text = message
        // 只有首页显示房子，否则不显示
        smallImageView.hidden = !isHome

        if isHome {
            smallImageView.image = UIImage(named: "visitordiscover_feed_image_house")
            bigImageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            // 将房屋图片前置
            bringSubviewToFront(smallImageView)
        } else {
            bigImageView.image = UIImage(named: imageName)
            // 将遮罩图片前置
            sendSubviewToBack(maskImageView)
        }
        
    }
    
    // If you create a view object programmatically,
    // 如果使用代码创建视图对象
    // this method is the designated initializer for the UIView class.
    // 这个方法就是UIView类的指定构造器
    //
    // Subclasses can override this method to perform any custom initialization
    // 子类可以重写这个方法
    // but must call super at the beginning of their implementation.
    // 但是必须在最一开始调用父类的是实现
    //
    // If you use Interface Builder to design your interface,
    // this method is not called when your view objects are subsequently loaded from the nib file.
    //
    // Objects in a nib file are reconstituted and then initialized using their initWithCoder: method,
    // which modifies the attributes of the view to match the attributes stored in the nib file.
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        
        // 设置UI
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置UI
    private func setupUI() {
        // ********************背景颜色********************
        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1 )
        
        // 添加子视图
        addSubview(smallImageView)
        addSubview(bigImageView)
        addSubview(maskImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 为内层imageView添加约束
        addConstraint(NSLayoutConstraint(
            item: smallImageView,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1,
            constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: smallImageView,
            attribute: NSLayoutAttribute.CenterY,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self,
            attribute: NSLayoutAttribute.CenterY,
            multiplier: 1,
            constant: -40)
        )
        
        // 为遮罩视图添加约束
        addConstraint(NSLayoutConstraint(
            item: maskImageView,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self,
            attribute: NSLayoutAttribute.Width,
            multiplier: 1, constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: maskImageView,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self,
            attribute: NSLayoutAttribute.Top,
            multiplier: 1, constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: maskImageView,
            attribute: NSLayoutAttribute.Bottom,
            relatedBy: NSLayoutRelation.Equal,
            toItem: loginButton,
            attribute: NSLayoutAttribute.Bottom,
            multiplier: 1, constant: 0)
        )
        
        
        // 为外层imageView添加约束
        addConstraint(NSLayoutConstraint(
            item: bigImageView,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: smallImageView,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1,
            constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: bigImageView,
            attribute: NSLayoutAttribute.CenterY,
            relatedBy: NSLayoutRelation.Equal,
            toItem: smallImageView,
            attribute: NSLayoutAttribute.CenterY,
            multiplier: 1,
            constant: 0)
        )
        
        // 为文本标签添加约束
        addConstraint(NSLayoutConstraint(
            item: messageLabel,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: bigImageView,
            attribute: NSLayoutAttribute.Bottom,
            multiplier: 1,
            constant: 8)
        )
        
        addConstraint(NSLayoutConstraint(
            item: messageLabel,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: bigImageView,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1,
            constant: 0)
        )
        
        
        // 为登录按钮添加约束
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: NSLayoutAttribute.Leading,
            relatedBy: NSLayoutRelation.Equal,
            toItem: messageLabel,
            attribute: NSLayoutAttribute.Leading,
            multiplier: 1,
            constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: messageLabel,
            attribute: NSLayoutAttribute.Bottom,
            multiplier: 1,
            constant: 8)
        )
        
        // 为注册按钮添加约束
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: NSLayoutAttribute.Trailing,
            relatedBy: NSLayoutRelation.Equal,
            toItem: messageLabel,
            attribute: NSLayoutAttribute.Trailing,
            multiplier: 1,
            constant: 0)
        )
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: loginButton,
            attribute: NSLayoutAttribute.Top,
            multiplier: 1,
            constant: 0)
        )
    }
    
    func registerButtonClick() {
        // 调用代理方法
        delegate?.visitorViewRegisterButtonClicked!()
    }
    
    func loginButtonClick() {
        // 调用代理方法
        delegate?.visitorViewLoginButtonClicked!()
    }
    
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        bigImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    /// 内层imageView
    private lazy var smallImageView: UIImageView = {
        let imageView = UIImageView(image: nil)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
        }()
    
    /// 外层imageView
    private lazy var bigImageView: UIImageView = {
        let imageView = UIImageView(image: nil)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
        }()
    
    /// 文本标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        // 宽度约束
        label.addConstraint(NSLayoutConstraint(
            item: label,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0, constant: 240)
        )
        
        // 高度约束
        label.addConstraint(NSLayoutConstraint(
            item: label,
            attribute: NSLayoutAttribute.Height,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0,
            constant: 40)
        )
        
        label.text = "sfdsfsdfsdhfdsahfkhdskfhakdshfafffffffff"
        label.numberOfLines = 2
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        label.sizeToFit()
        return label
        }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.System)
        button.translatesAutoresizingMaskIntoConstraints = false
        // 宽度约束
        button.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0,
            constant: 100)
        )
        
        // 高度约束
        button.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: NSLayoutAttribute.Height,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0,
            constant: 40)
        )
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.adjustsImageWhenHighlighted = false
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 绑定targer
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.System)
        button.translatesAutoresizingMaskIntoConstraints = false
        // 宽度约束
        button.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0,
            constant: 100)
        )
        // 高度约束
        button.addConstraint(NSLayoutConstraint(
            item: button,
            attribute: NSLayoutAttribute.Height,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1.0,
            constant: 40)
        )
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.adjustsImageWhenHighlighted = false
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 绑定targer
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    // 遮罩视图
    private lazy var maskImageView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()
    
}
